<?php

namespace Mvc\Core\Model;

use Mvc\Application;

/**
 * Created by PhpStorm.
 * User: 554665488
 * Date: 2018-6-22
 * Time: 6:05
 * @description:使用观察者模式
 * @Author: yfl
 * @QQ 554665488
 */
class Model
{
    protected $observers = array();//保存观察者对象

    /**初始化观察者
     * Model constructor.
     */
    public function __construct()
    {
//        echo get_class($this);//  Mvc\App\Model\User
        $name = strtolower(str_replace('Mvc\\App\\Model\\', '', get_class($this)));
        $configModel=Application::instance()->config['model'];

        if (!empty($configModel)) {
            $observers = $configModel[$name]['observer'];
            foreach ($observers as $class) {
                $this->observers[] = new $class;
            }
        }
    }

    /**
     * @description:通知配置的观察者去干活
     * @time:2018-6-22 7:07
     * @Author: yfl
     * @Remark:
     * @param $event
     */
    public function notify($event)
    {
//        var_dump($this->observers);
        foreach ($this->observers as $observer) {
            $observer->update($event);
        }
    }

}